package com.xikeda.controller;

import com.alibaba.fastjson.JSON;
import com.xikeda.dao.QueryDetails;
import com.xikeda.model.*;
import com.xikeda.service.imp.*;
import com.xikeda.tools.CursorhomeDTO;
import com.xikeda.tools.ResiciptmasterDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author Xiao Linqing
 * @date 2020/6/6 23:12
 */
@Controller
public class CourierLoginController {
    @Autowired
    private CourierLoginService courierLoginService;
    @Autowired
    private UserLoginService userLoginService;
    @Autowired
    private InquiryService inquiryService;
    @Autowired
    private  QueryDetailsService queryDetailsService;
    @Autowired
    private ExpressdeliveryService expressdeliveryService;
    @Autowired
    private AverageService averageService;
    @Autowired
    private confirmreceiptService confirmreceiptService;
    @RequestMapping("/index")
    public String InquiryExpress1(HttpServletRequest request, HttpServletResponse response){

        return "begin";
    }

    @RequestMapping("/login")
    public String Logins(HttpServletRequest request,HttpServletResponse response){

        return "login";
    }

    @RequestMapping("/xuanzedenglu")
    public String Logins1(HttpServletRequest request, HttpServletResponse response, Model model) throws IOException {
        String telephone=request.getParameter("phone");
        String password=request.getParameter("password");
        String ide=request.getParameter("ide");
        if(ide!=null&&!ide.equals("")){
            if(ide.equals("user")){
                System.out.println("进入用户页面");
                System.out.println("用户输入电话号码"+telephone);
                System.out.println("用户输入密码"+password);
                String passwordRelly = userLoginService.finduserpassword(telephone);
                if(passwordRelly.equals(password) && passwordRelly != null){
                    List<ResiciptmasterDTO>list = new ArrayList<>();
                    list = ResiciptmasterDTOList(telephone,password);
                    int id = inquiryService.findfindid(telephone);
                    model.addAttribute("id",id);
                    model.addAttribute("list",list);
                    return "resiciptmaster";
                }
            }
            if(ide.equals("post")){
                System.out.println("进入快递员页面");
                System.out.println("用户输入电话号码"+telephone);
                System.out.println("用户输入密码"+password);
                String passwordRelly= courierLoginService.findpassword(telephone);
                if(passwordRelly.equals(password) && passwordRelly != null){
                    List<CursorhomeDTO>list=new ArrayList<>();
                    list=CursorhomeDTOList(telephone,password);
                    int id=expressdeliveryService.findcourierid(telephone);
                    List<EvaluateTable> list3=averageService.average(Integer.toString(id));
                    int q=0;
                    int x=0;
                    if (list3 != null && list3.size() > 0) {
                        for (EvaluateTable i : list3) {
                            q = q + i.getCourier_level();
                        }
                        x = q / list3.size();
                        System.out.println();
                        System.out.println("输出的平均值是" + x);
                        model.addAttribute("average",x);
                    }
                    model.addAttribute("id",id);
                    model.addAttribute("list",list);
                    return "cursorhome";
                }
            }

        }
        return "login";

    }




    public List<ResiciptmasterDTO> ResiciptmasterDTOList (String teltphone,String passward){
        List<ResiciptmasterDTO>resultList = new ArrayList<>();
        int id = inquiryService.findfindid(teltphone);
        List<DeliveryTable> list=inquiryService.FindID(Integer.toString(id));
        for(DeliveryTable li : list ){
            ResiciptmasterDTO resiciptmasterDTO = new ResiciptmasterDTO();
            resiciptmasterDTO.setCommodity_name(li.getCommodity_name());
            resiciptmasterDTO.setCourier_id(li.getCourier_id());
            CourierTable courierTable=inquiryService.findcourier(String.valueOf(li.getCourier_id()));
            resiciptmasterDTO.setCourier_true_name(courierTable.getCourier_true_name());
            resiciptmasterDTO.setTelephone(courierTable.getTelephone());
            resiciptmasterDTO.setService_level(courierTable.getService_level());
            resultList.add(resiciptmasterDTO);

        }
        return resultList;
    }
    private List<CursorhomeDTO> CursorhomeDTOList(String telephone, String password) {
        List<CursorhomeDTO> backList=new ArrayList<>();
        int id=expressdeliveryService.findcourierid(telephone);
        System.out.println("快递员ID为"+id);
        List<DeliveryTable> list=expressdeliveryService.Finddeliverygoods(Integer.toString(id));
        for(DeliveryTable lx:list){
            CursorhomeDTO cursorhomeDTO=new CursorhomeDTO();
            cursorhomeDTO.setCommodity_name(lx.getCommodity_name());
            System.out.print("商品名称: "+lx.getCommodity_name()+";");

            //List<DeliveryTable> list4=confirmreceiptService.receipt(Integer.toString(lx.getDelivery_id()));
            System.out.print("用户ID: "+lx.getUser_id()+";");
            //cursorhomeDTO.setUser_id(lx.getUser_id());
            List<UsernaadTable> list1=this.queryDetailsService.Finduser(String.valueOf(lx.getUser_id()));
            if(list1!=null&&list1.size()>0){
               for(UsernaadTable lu:list1) {
                   System.out.print(lu.getCourier_address()+" ");
                   System.out.print(lu.getTelephone()+" ");
                   System.out.println(lu.getTrue_name()+" ");
                   System.out.println(lu.getDelivery_id()+" ");
                   cursorhomeDTO.setCourier_address(lu.getCourier_address());
                   cursorhomeDTO.setTelephone(lu.getTelephone());
                   cursorhomeDTO.setTrue_name(lu.getTrue_name());
                   cursorhomeDTO.setDelivery_id(lu.getDelivery_id());
               }
            }
            backList.add(cursorhomeDTO);
        }
        return backList;
    }
}
   /* @RequestMapping("/courierlogin")
    @ResponseBody
    public String courierlogin(HttpServletRequest request,HttpServletResponse response) throws Exception {
        String telephone = request.getParameter("telephone");
        String courier_password = request.getParameter("courier_password");
        System.out.println(telephone);
        System.out.println(courier_password);
        List<CourierTable> list = this.getCourierLoginService().findpassword(telephone);
        if (list != null && list.size() > 0) {
            for (CourierTable i : list) {
                System.out.println(i.getCourier_password());
                System.out.println(courier_password);
                if(i.getCourier_password().equals(courier_password)){
                    System.out.println("登录成功");
                    return "cursorhome";
                }else{
                    System.out.println("登录失败");
                    return "login";
                }
            }
        }
    return"";
    }*/
    /*@RequestMapping("/userlogin")
    @ResponseBody
    public String userlogin(HttpServletRequest request1, HttpServletRequest request2, HttpServletResponse response) throws Exception {
        String telephone = request1.getParameter("telephone");
        String password = request2.getParameter("password");
        System.out.println(telephone);
        System.out.println(password);
        System.out.println(1);
        List<UserTable> list = this.getUserLoginService().finduserpassword(telephone);
        if (list != null && list.size() > 0) {
            for (UserTable i : list) {
                System.out.println(password);
                System.out.println(i.getPassword());
                if(i.getPassword().equals(password)){
                    System.out.println("登录成功");
                    return "resiciptmaster";
                }else{
                    System.out.println("登录失败");
                    return "login";
                }
            }
        }
        return "";
    }*/

